home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / timer.c < prev    next >
C/C++ Source or Header  |  1985-12-04  |  4KB  |  138 lines

  1.  
  2. /* ******************************************************************** */ 
  3. /* Exec Support Functions:  CreateTimer, WaitTimer, DeleteTimer         */
  4. /*
  5. /* CreateTimer() returns a pointer to an IOStdReq structure, set up
  6.  * for communications with the timer device.  Returns NULL if it
  7.  * cannot allocate the timer or get enough memory or signals.
  8.  * 
  9.  * WaitTimer(IOStdReq,seconds,microseconds) uses the communications
  10.  * block from CreateTimer().  It puts your task to sleep until the
  11.  * timer counts down at least this amount of time.  
  12.  *
  13.  * SetTimer(IOStdReq,seconds,microseconds) returns a pointer to a port
  14.  * (copied from the IOStdReq) to which the timer returns the message
  15.  * block when the timeout has completed.  It uses SendIO to transmit
  16.  * the IORequest.  This means your task can go on to something else,
  17.  * then execute WaitPort(timerReplyPort), going to sleep until the
  18.  * timer has timed out.  You will still have to do a GetMsg(timerReplyPort)
  19.  * after your task awakens, where with WaitTimer this is not necessary.
  20.  *
  21.  * DeleteTimer(IOStdReq) uses the block from CreateTimer() to free up
  22.  * memory and signal bits that have been allocated for this purpose.
  23.  *
  24.  * syntax Summary:  struct IOStdReq *CreateTimer();
  25.  *                  int WaitTimer();
  26.  *                  struct Port *SetTimer();
  27.  *                  int DeleteTimer();
  28.  */
  29.  
  30.  
  31. #include <exec/types.h>
  32. #include <exec/lists.h>
  33. #include <exec/nodes.h>
  34. #include <exec/ports.h>
  35. #include <exec/io.h>
  36. #include <exec/devices.h>
  37. #include <devices/timer.h>
  38.  
  39. #define SECONDS io_Actual
  40. #define MICROSECONDS io_Length
  41. /* redefine fields in IOStdReq so as to match requirements of a timeval */
  42.  
  43.  
  44. extern struct Port *CreatePort();
  45. extern struct IOStdReq *CreateStdIO();
  46.  
  47. struct IOStdReq 
  48. *CreateTimer()          /* return a pointer to an IOStdReq if 
  49.                          * it was possible to allocate a new
  50.                          * timer */
  51. {
  52.         SHORT error;
  53.         struct Port *timerport;
  54.         struct IOStdReq *timermsg;
  55.         timerport = CreatePort(0,0);
  56.         if (timerport == NULL)
  57.                 return(NULL);  /* Error during CreatePort */
  58.         timermsg = CreateStdIO(timerport);
  59.         if (timermsg == NULL)
  60.                 {
  61.                 DeletePort(timerport);
  62.                 return(NULL);  /* Error during CreateStdIO */
  63.                 }
  64.         error = OpenDevice(TIMERNAME, UNIT_MICROHZ, timermsg, 0);
  65.         if (error != 0)
  66.                 {
  67.                 DeleteStdIO(timermsg);
  68.                 DeletePort(timerport);
  69.                 return(NULL);  /* Error during OpenDevice */
  70.                 }
  71.         return(timermsg);
  72. }
  73.  
  74. struct Port 
  75. *SetTimer(whichtimer,seconds,microseconds)
  76. ULONG seconds,microseconds;
  77. struct IOStdReq *whichtimer;
  78. {
  79.         struct Port *tempPort;
  80.         tempPort = whichtimer->io_Message.mn_ReplyPort;
  81.         whichtimer->io_Command = TR_ADDREQUEST;   /* add a new timer request */
  82.         whichtimer->SECONDS = seconds;            /* seconds */
  83.         whichtimer->MICROSECONDS = microseconds;  /* microseconds */
  84.         SendIO(whichtimer);
  85.         return(tempPort);
  86. }
  87.  
  88. int
  89. WaitTimer(whichtimer,seconds,microseconds)
  90. ULONG seconds,microseconds;
  91. struct IOStdReq *whichtimer;
  92. {
  93.         whichtimer->io_Command = TR_ADDREQUEST;   /* add a new timer request */
  94.         whichtimer->SECONDS = seconds;            /* seconds */
  95.         whichtimer->MICROSECONDS = microseconds;  /* microseconds */
  96.         DoIO(whichtimer);
  97.         return(0);
  98. }
  99.  
  100. int
  101. DeleteTimer(whichtimer)
  102. struct IOStdReq *whichtimer;
  103. {
  104.         struct Port *whichport;
  105.         whichport = whichtimer->io_Message.mn_ReplyPort;
  106.         DeleteStdIO(whichtimer);
  107.         DeletePort(whichport);
  108.         return(0);      
  109. }
  110.  
  111.  
  112.  
  113.  
  114. *************************************************************************
  115. *   HandlerInterface()
  116. *
  117. *   This code is needed to convert the calling sequence performed by
  118. *   the input.task for the input stream management into something
  119. *   that a C program can understand.
  120. *
  121. *   This routine expects a pointer to an InputEvent in A0, a pointer
  122. *   to a data area in A1.  These values are transferred to the stack
  123. *   in the order that a C program would need to find them.  Since the
  124. *   actual handler is written in C, this works out fine. 
  125.  
  126.     XREF        _myhandler
  127.     XDEF        _HandlerInterface
  128.  
  129. _HandlerInterface:
  130.     MOVEM.L     A0/A1,-(A7)
  131.     JSR         _myhandler
  132.     ADDQ.L      #8,A7
  133.     RTS
  134.  
  135.     END
  136.  
  137.  
  138.